//
//  HWCommentKeyboardView.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/5.
//

import UIKit
import IQKeyboardManagerSwift
class HWCommentKeyboardView: UIView {
    var bgView:UIView!
    var sendButton:UIButton!
    @objc var textField:UITextField!
    @objc var sendText:((_:String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        sendButton.layoutIfNeeded()
//        let gradientColors = [UIColor.colorWithHexString(hexString: "#6766FC").cgColor,
//                              UIColor.colorWithHexString(hexString: "#AC25FB").cgColor,
//                              
//        ]
//        
//        //创建CAGradientLayer对象并设置参数
//        let gradientLayer = CAGradientLayer()
//        gradientLayer.colors = gradientColors
//        
//        //设置渲染的起始结束位置（横向渐变）
//        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
//        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
//        
//        //设置其CAGradientLayer对象的frame，并插入view的layer
//        gradientLayer.frame = sendButton.bounds
//        sendButton.layer.insertSublayer(gradientLayer, at: 0)
        sendButton.layer.cornerRadius = 17
        sendButton.layer.masksToBounds = true
        
        UIColor.gradientColor(view: sendButton, colors: [
            UIColor.colorWithAlphaHexString(hexString: "#FFB60A", alpha: 1).cgColor,
            UIColor.colorWithAlphaHexString(hexString: "#F66A05", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
    }
    
}
extension HWCommentKeyboardView {
    private func initUI() -> Void {
        
        backgroundColor = .white
        
        IQKeyboardManager.shared.toolbarBarTintColor = .white
        IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "收起"
        IQKeyboardManager.shared.keyboardDistanceFromTextField = 100
//        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
//        IQKeyboardManager.shared.enableAutoToolbar = false
//        [IQKeyboardManager sharedManager].shouldToolbarUsesTextFieldTintColor = NO;
        bgView = UIView()
        bgView.backgroundColor = .colorWithHexString(hexString: "#F6F7F9")
        bgView.layer.cornerRadius = 17
        bgView.layer.masksToBounds = true
        addSubview(bgView)
        
        textField = UITextField()
        textField.font = UIFont.systemFont(ofSize: 14)
        textField.textColor = .colorWithHexString(hexString: "#333333")
        textField.placeholder = "请输入评论..."
//        textField.backgroundColor = .white
        textField.tintColor = .white
        bgView.addSubview(textField)
        
        sendButton = .init(type: .custom)
        sendButton.setTitle("发送", for: UIControl.State.normal)
        sendButton.setTitleColor(.white, for: UIControl.State.normal)
        sendButton.titleLabel?.font = .systemFont(ofSize: 16)
        sendButton.backgroundColor = UIColor.blue
        sendButton.layer.cornerRadius = 17
        sendButton.addTarget(self, action: #selector(sendButtonClick), for: UIControl.Event.touchUpInside)
        addSubview(sendButton)
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.height.equalTo(34)
            make.left.equalTo(15)
            make.top.equalTo(10)
            make.right.equalTo(sendButton.snp.left).offset(-10)
            make.bottom.equalTo(-10)
        }
        
        textField.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(5)
            make.bottom.right.equalTo(-5)
        }
        
        sendButton.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.height.equalTo(34)
            make.width.equalTo(63)
            make.top.equalTo(bgView)
        }
        
        sendButton.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
        sendButton.setContentCompressionResistancePriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
        
    }
}

extension HWCommentKeyboardView {
    @objc func sendButtonClick() -> Void {
        sendText!(textField.text ?? "")
    }
}
